package com.controller.supply;

import com.alibaba.fastjson.JSONObject;
import com.common.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.common.DateUtils;
import com.common.http.HttpResult;
import com.controller.task.SurpviseController;
import com.model.supply.SupplyInfoModel;
import com.model.supply.SupplyInfoVoModel;
import com.service.supply.InfoService;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

/**
 * 供应商信息管理
 */
@Controller("supplyInfo")
@RequestMapping(value = "/api")
public class InfoController {
    static final Logger logger = LoggerFactory.getLogger(InfoController.class);

    @Autowired
    InfoService infoService;

    /**
     * 查询列表
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/supply/info/selectList", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult selectSupplyList(@RequestParam Map<String, Object> map) {
        return infoService.selectSupplyList(map);
    }

    /**
     * 查询供应商编码、名称_COMBOGRID
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "/supply/info/selectSupplyForCombo", method = {RequestMethod.POST})
    @ResponseBody
    public List selectSupplyForCombo(@RequestParam Map<String, Object> map) {
        logger.info("参数=====" + JSONObject.toJSONString(map.get("q")));
        String q = null == map.get("q") ? "" : map.get("q").toString();
        if (!StringUtils.isNullOrEmpty(q)) {
            map.put("infName", q);
        }
        return infoService.selectSupplyForCombo(map);
    }

    /**
     * 查询详情
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "supply/info/selectInfor", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult selectInfor(@RequestParam String id) {
        return infoService.selectInfor(id);
    }

    /**
     * 重置密码
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "supply/resetPwd", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult resetPwd(@RequestParam String loginName, @RequestParam String token) {
        return infoService.resetPwd(loginName);
    }

    /**
     * 重置密码
     *
     * @param map
     * @return
     */
    @RequestMapping(value = "supply/sendMail", method = {RequestMethod.GET})
    @ResponseBody
    public HttpResult sendMail(@RequestParam String loginName) {
        return infoService.sendMail(loginName);
    }

    /**
     * 保存
     *
     * @param
     * @return
     */
    @RequestMapping(value = "supply/info/save", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult save(@RequestBody SupplyInfoVoModel infoModel) {
        return infoService.save(infoModel);
    }

    /**
     * 提交
     *
     * @param
     * @return
     */
    @RequestMapping(value = "supply/info/commit", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult commit(@RequestBody SupplyInfoVoModel infoModel) {
        return infoService.commit(infoModel);
    }

    /**
     * 推送oa
     *
     * @param
     * @return
     */
    @RequestMapping(value = "supply/info/pushOa", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult pushOa(@RequestBody SupplyInfoVoModel infoModel) {
        return infoService.pushOa(infoModel);
    }

    /**
     * 推送mdg
     *
     * @param
     * @return
     */
    @RequestMapping(value = "supply/info/pushMdg", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult pushMdg(@RequestBody SupplyInfoVoModel infoModel) {
        return infoService.pushMdg(infoModel);
    }

    /**
     * 审核不通过
     *
     * @param infoModel
     * @return
     */
    @RequestMapping(value = "supply/info/checkNo", method = {RequestMethod.POST})
    @ResponseBody
    public HttpResult checkNo(@RequestParam String infId) {
        return infoService.checkNo(infId);
    }

    /**
     * 删除询价
     *
     * @param
     * @return
     */
    @RequestMapping(value = "supply/info/delete", method = {RequestMethod.DELETE})
    @ResponseBody
    public HttpResult delete(@RequestParam String id) {
        return infoService.delete(id);
    }

    /**
     * 导出
     *
     * @param map
     * @param response
     * @throws IOException
     */
    @RequestMapping(value = "supply/info/export")
    public void export(@RequestParam Map<String, Object> map, HttpServletResponse response) throws IOException {
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String fileName = URLEncoder.encode("采购订单-" + DateUtils.getCurrentDate(), "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");
        infoService.export(map, response);

    }

}
